<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommonController {
    public function indexAction(){
        //查询数据库数据条数
        $sql = "select count(id) count from user";

        $count = current(M()->query($sql))['count'];

        // 分页类
        $Page  = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $show  = $Page->show();// 分页显示输出

        $sql = "select * from user order by id limit {$Page->firstRow},{$Page->listRows}";

        $res = M()->query($sql);

        foreach ($res as &$v) {

            $v['reg_time'] = date('Y-m-d H:i:s',$v['reg_time']);

        }

        $this->assign('show',$show);
        $this->assign('count',$count);
        $this->assign('res',$res);
        $this->display();
    }

    public function welcomeAction()
    {
    	$this->display();
    }

    public function showAction(){
        $id = I('get.id');
        $sql = "select * from user where id='{$id}'";
        $res = current(M()->query($sql));
        // var_dump($res);
        $res['reg_time'] = date('Y-m-d H:i:s',$res['reg_time']);
        $this->assign('res',$res);
        $this->display();
    }

    public function addAction(){
        if (IS_POST) {
            var_dump($_POST);
            $username = I('post.username','');
            $password = I('post.password','');
            $sex = I('post.sex','');
            $birthday = I('post.birthday','');
            $email = I('post.email','');
            $face = I('post.face','');
            $city = I('post.city','');
            $introduce = I('post.introduce','');
            $emailsql = "select email from user where email='{$email}'";
            if (M()->query($emailsql)) {
                $this->error('该邮箱已存在',U('admin/user/index'));
            }


            //TP框架封装好的头像上传类，这里可以设置配置数据
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
            $upload->savePath  =      ''; // 设置附件上传（子）目录
            // 上传文件 
            $info   =   $upload->upload();
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{
                foreach($info as $file){
                    // var_dump($file);exit;
                    $face = "/Uploads/".$file['savepath'].$file['savename'];
                    // var_dump($face);exit;
                }
            }

            $sql = "insert into user (username,password,sex,face,birthday,email,city,introduce) value ('{$username}','{$password}','{$sex}','{$face}','{$birthday}','{$email}','{$city}','{$introduce}')";
            if (M()->execute($sql)) {
                $this->success('添加成功',U('admin/user/index'));
            }else{
                $this->error('添加失败',U('admin/user/index'));
            }
        }
    	$this->display();
    }
    

    public function editAction() {
        if(IS_POST) {
            var_dump($_POST);
            $username = I('post.username','');
            $password = I('post.password','');
            $id = I('post.id','');
            $face = I('post.face','');
            $sex = I('post.sex','');
            $birthday = I('post.birthday','');
            $city = I('post.city','');
            $email = I('post.email','');
            $introduce = I('post.introduce','');

            if (empty($id)) {
                $this->error('非法请求',U('admin/user/index'));
            }
            //TP框架封装好的头像上传类，这里可以设置配置数据
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
            $upload->savePath  =      ''; // 设置附件上传（子）目录
            // 上传文件 
            $info   =   $upload->upload();
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{
                foreach($info as $file){
                    // var_dump($file);exit;
                    $face = "/Uploads/".$file['savepath'].$file['savename'];
                    // var_dump($face);
                }
            }
            $sql = "update user set username='{$username}',password='{$password}',email='{$email},'face='{$face}',sex='{$sex}',birthday='{$birthday}',city='{$city}',introduce='{$introduce}' where id='{$id}'";
            var_dump($sql);exit;
            $res = M()->execute($sql);
            if ($res) {
                $this->success('修改成功',U('admin/user/index'));
            }else{
                $this->success('修改失败',U('admin/user/index'));
            }
        }else{
            $id = I('get.id','');
            if (empty($id)) {
                $this->error('非法请求',U('admin/user/index'));
            }else{
                $sql = "select * from user where id='{$id}'";
                $res = current(M()->query($sql));
                // var_dump($res);
            }
        }        
        
        $this->assign('res',$res);
        $this->display();
    }

    //异步
    // public function delAction() {
    //     $id = I('post.theuserid');
    //     $sql = "delete from  user where id='{$id}'";
    //     M()->execute($sql);
    //     echo json_encode(1);
    // }

    public function delAction() {
        $id = I('get.id');
        $sql = "delete from  user where id='{$id}'";
        $res = M()->execute($sql);
        if($res){
            $this->success('删除成功',U('admin/user/index'));
        }else{
            $this->error('删除失败',U('admin/user/index'));
        }
    }

    //删除的用户
    public function deledAction() {
        $sql = "select count(id) count from user where status='-1'";

        $count = current(M()->query($sql))['count'];

        // 分页类
        $Page  = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $show  = $Page->show();// 分页显示输出

        $sql = "select * from  user where status='-1' limit {$Page->firstRow},{$Page->listRows}";

        $res = M()->query($sql);

        var_dump($res);

        foreach ($res as &$v) {

            $v['reg_time'] = date('Y-m-d H:i:s',$v['reg_time']);

        }

        $this->assign('show',$show);
        $this->assign('count',$count);
        $this->assign('res',$res);
        $this->display();
    }

    //黑名单
    public function blackAction() {
        $sql = "select count(id) count from user where status='-2'";

        $count = current(M()->query($sql))['count'];

        // 分页类
        $Page  = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $show  = $Page->show();// 分页显示输出

        $sql = "select * from  user where status='-2' limit {$Page->firstRow},{$Page->listRows}";

        $res = M()->query($sql);

        var_dump($res);

        foreach ($res as &$v) {

            $v['reg_time'] = date('Y-m-d H:i:s',$v['reg_time']);

        }

        $this->assign('show',$show);
        $this->assign('count',$count);
        $this->assign('res',$res);
        $this->display();
    }

    //停用状态
    public function stopAction() {
        $id = I('get.id');        
        $sql = "update user set status='1' where id='{$id}'";
        if (M()->execute($sql)) {
            $this->success('停用成功',U('admin/user/index'));
        }else{
            $this->error('停用失败',U('admin/user/index'));
        }       
    }

    //启用状态
    public function upAction() {
        $id = I('get.id');        
        $sql = "update user set status='0' where id='{$id}'";
        if (M()->execute($sql)) {
            $this->success('启用成功',U('admin/user/index'));
        }else{
            $this->error('启用失败',U('admin/user/index'));
        }      
    }

    //恢复黑名单
    public function whiteAction() {
        $id = I('get.id');        
        $sql = "update user set status='0' where id='{$id}'";
        if (M()->execute($sql)) {
            $this->success('启用成功',U('admin/user/index'));
        }else{
            $this->error('启用失败',U('admin/user/index'));
        }      
    }
}